package cn.iocoder.yudao.module.system.framework.operatelog.core;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.module.system.enums.DictTypeConstants;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mzt.logapi.service.IParseFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class InspectionItemFunction implements IParseFunction {

    public static final String NAME = "getInspectionItemFunction";

    @Override
    public boolean executeBefore() {
        return true; // 先转换值后对比
    }

    @Override
    public String functionName() {
        return NAME;
    }

    @Override
    public String apply(Object value) {
        if (StrUtil.isEmptyIfStr(value)) {
            return "";
        }
        String string = value.toString();

        String[] split = string.replace("【", "").replace("】", "").split("修改为");
        String inspectionItem = "";

        String title = "";

        String data1 = "";
        int i = 0;
        ObjectMapper mapper = new ObjectMapper();



        try {

            for (String s : split) {

                System.out.println(s);
                // 1. 解析外层 JSON
                JsonNode rootNode = mapper.readTree(s);
                // 2. 获取 fileds 数组
                JsonNode filedsArray = rootNode.get("fileds");


                if (rootNode.has("data")) {
                    JsonNode data = rootNode.get("data");
                    data1 = data.toString();

                }else {
                    data1 = null;
                }

                //删除细节，只保留文字提示
                if (data1 != null) {
                    data1 = "更新了图片";
                }

                //判断修改前后的数据
                i++;
                // 3. 遍历每个字段
                for (JsonNode fieldNode : filedsArray) {
                    // 4. 解析内层 JSON 字符串
                    JsonNode innerNode = mapper.readTree(fieldNode.asText());

                    if (innerNode.has("title")){
                        // 5. 提取 title 值
                        title = innerNode.get("title").asText();
                    }else{
                        title = "";
                    }
                    inspectionItem = inspectionItem + "【" + title + "】";
                }
            }

            inspectionItem = inspectionItem + "【" + data1 + "】";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return inspectionItem;

    }
}
